#!/usr/bin/env python3

# Updates static files for production.


import os
import argparse
import shutil
import sys

# We need settings so we can figure out where the prod-static directory is.
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import scripts.lib.setup_path_on_import
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
from django.conf import settings
from scripts.lib.node_cache import setup_node_modules
from scripts.lib.zulip_tools import run

# check for the venv
from lib import sanity_check
sanity_check.check_venv(__file__)

parser = argparse.ArgumentParser()
parser.add_argument('--prev-deploy', metavar='DIR',
                    help='a previous deploy from which to reuse files if possible')
parser.add_argument('--authors-not-required', action='store_true', default=False,
                    help='Authors files need not be updated')
args = parser.parse_args()
prev_deploy = args.prev_deploy

os.chdir(settings.DEPLOY_ROOT)

# Redirect child processes' output to a log file (most recent run only).
os.makedirs("var/log", exist_ok=True)
fp = open('var/log/update-prod-static.log', 'w')

# Install node packages
setup_node_modules(production=True, stdout=fp, stderr=fp)

# Build emoji
run(['./tools/setup/emoji/build_emoji'], stdout=fp, stderr=fp)

# Inline CSS in emails
run(['./tools/inline-email-css'], stdout=fp, stderr=fp)

# Copy over static files from the zulip_bots package
run(['./tools/setup/generate_zulip_bots_static_files.py'], stdout=fp, stderr=fp)

# Generate custom icon webfont
run(['./tools/setup/generate-custom-icon-webfont'], stdout=fp, stderr=fp)

# Build pygment data
run(['./tools/setup/build_pygments_data'], stdout=fp, stderr=fp)

# Compile Handlebars templates and minify JavaScript.
run(['./tools/minify-js'] + (['--prev-deploy', prev_deploy] if prev_deploy else []),
    stdout=fp, stderr=fp)

# Copy the KaTeX files outside node_modules
os.makedirs(os.path.join(settings.STATIC_ROOT, 'node_modules/katex/dist/'),
            exist_ok=True)

shutil.copy('node_modules/katex/dist/katex.css',
            os.path.join(settings.STATIC_ROOT, 'node_modules/katex/dist/'))

run(['cp', '-R', 'node_modules/katex/dist/fonts',
     os.path.join(settings.STATIC_ROOT, 'node_modules/katex/dist/fonts')],
    stdout=fp, stderr=fp)

CSS_FILES = [
    'node_modules/simplebar/dist/simplebar.css',
    'node_modules/perfect-scrollbar/css/perfect-scrollbar.css',
    'node_modules/flatpickr/dist/flatpickr.css',
    'node_modules/flatpickr/dist/plugins/confirmDate/confirmDate.css',
]

# Copy CSS files in node_modules to prod-static/serve
for css_file in CSS_FILES:
    os.makedirs(os.path.join(settings.STATIC_ROOT, os.path.dirname(css_file)), exist_ok=True)
    shutil.copyfile(css_file, os.path.join(settings.STATIC_ROOT, css_file))

# Collect the files that we're going to serve; this creates prod-static/serve.
run(['./manage.py', 'collectstatic', '--no-default-ignore',
     '--noinput', '-i', 'assets', '-i' 'node_modules', '-i', 'styles', '-i', 'templates'],
    stdout=fp, stderr=fp)

if not settings.PRODUCTION:
    # When building a release tarball, we need to move staticfiles.json
    shutil.move('prod-static/serve/staticfiles.json', 'staticfiles.json')

# Compile translation strings to generate `.mo` files.
run(['./manage.py', 'compilemessages'], stdout=fp, stderr=fp)

# Move the source maps out of the serve/ directory and into their
# proper place.
if os.path.exists('prod-static/source-map'):
    shutil.rmtree('prod-static/source-map')

# Needed if PRODUCTION
os.makedirs('prod-static', exist_ok=True)

shutil.move(os.path.join(settings.STATIC_ROOT, 'source-map'), 'prod-static/source-map')

# Move language_options.json to the production release
run(['cp', '-aT', 'static/locale', os.path.join(settings.STATIC_ROOT, 'locale')],
    stdout=fp, stderr=fp)

# Generate /team page markdown for authors
authors_cmd = ['./tools/update-authors-json']
if os.environ.get("TRAVIS"):
    authors_cmd.append("--use-fixture")
if args.authors_not_required:
    authors_cmd.append("--not-required")
run(authors_cmd, stdout=fp, stderr=fp)

fp.close()
